package com.hnchances.wjb.Service.Impl;

import com.hnchances.wjb.Service.MsmService;
import com.hnchances.wjb.checksum.CheckSumBuilder;
import com.hnchances.wjb.entity.Message;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MsmServiceImpl implements MsmService {
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public String code(String phone) throws IOException {
        //发送验证码的请求路径URL
        final String
                SERVER_URL="https://api.netease.im/sms/sendcode.action";
        //网易云信分配的账号，请替换你在管理后台应用下申请的Appkey
        final String
                APP_KEY="436800d7b365e8ece3f918ea325a2fb2";
        //网易云信分配的密钥，请替换你在管理后台应用下申请的appSecret
        final String APP_SECRET="a6e190234542";
        //随机数
        final String NONCE="123456";
        //短信模板ID
        final String TEMPLATEID="19519925";
        //手机号，接收者号码列表，JSONArray格式，限制接收者号码个数最多为100个
        String MOBILES=phone;
        //短信参数列表，用于依次填充模板，JSONArray格式，每个变量长度不能超过30字,对于不包含变量的模板，不填此参数表示模板即短信全文内容
        final String CODELEN="4";
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(SERVER_URL);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        /*
         * 参考计算CheckSum的java代码，在上述文档的参数列表中，有CheckSum的计算文档示例
         */
        String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);

        // 设置请求的header
        httpPost.addHeader("AppKey", APP_KEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        // 设置请求的的参数，requestBody参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        /*
         * 1.如果是模板短信，请注意参数mobile是有s的，详细参数配置请参考“发送模板短信文档”
         * 2.参数格式是jsonArray的格式，例如 "['13888888888','13666666666']"
         * 3.params是根据你模板里面有几个参数，那里面的参数也是jsonArray格式
         */
        nvps.add(new BasicNameValuePair("templateid", TEMPLATEID));
        nvps.add(new BasicNameValuePair("mobile", MOBILES));
        nvps.add(new BasicNameValuePair("codeLen", CODELEN));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        /*
         * 1.打印执行结果，打印结果一般会200、315、403、404、413、414、500
         * 2.具体的code有问题的可以参考官网的Code状态表
         */
        String entity = null;
        response.getEntity();
        entity = EntityUtils.toString(response.getEntity(), "utf-8");
        System.out.println(entity);
        //            调用send执行网易短信发送
        String send = entity;
//        将String转换为JSONObject
        JSONObject jsonobject = JSONObject.fromObject(send);
//        将json转换为message对象
        Message message = (Message) JSONObject.toBean(jsonobject, Message.class);

        redisTemplate.opsForValue().set(phone,message.getObj(), 10, TimeUnit.MINUTES);
        System.out.println("gasyd");
//        System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));

        return null;
    }
}
